//
//  RegistrationCodeView.m
//  Bees
//
//  Created by NowYouCanSee on 2019/1/25.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "RegistrationCodeView.h"
#import "LEEAlert.h"
#import <AssetsLibrary/ALAssetsLibrary.h>

@implementation RegistrationCodeView

+ (void)show{
    RegistrationCodeView *view = GET_VIEW_BY_NibName(@"RegistrationCodeView");
    UIView *costomV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth-AdaptedWidth(40), AdaptedWidth(600))];
    [costomV addSubview:view];
    view.frame = costomV.bounds;
    [LEEAlert alert].config
    .LeeHeaderInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeMaxWidth(costomV.width)
    .LeeMaxHeight(costomV.height)
    .LeeAddCustomView(^(LEECustomView *custom) {
        custom.view = costomV;
    }).LeeShow();
}
- (IBAction)closeBtnClick:(id)sender {
    [LEEAlert closeWithCompletionBlock:nil];
}
- (IBAction)copyBtnClick:(id)sender {
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
    [pab setString:@"a1022xz"];
    if (pab == nil) {
        [self makeToast:XLocalized(@"复制失败!")];
    }else
    {
        [self makeToast:XLocalized(@"复制成功!")];
    }
}

- (IBAction)saveCode:(id)sender {
    [self savePhoto];
}
-(UIImage *)captureImageFromView:(UIView *)view

{
    
    CGRect screenRect = [view bounds];
    
    UIGraphicsBeginImageContext(screenRect.size);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    [view.layer renderInContext:ctx];
    
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}
-(void)savePhoto

{
    
    UIImage * image = [self captureImageFromView:self.codeImageV];
    
    ALAssetsLibrary * library = [ALAssetsLibrary new];
    NSData * data = [image imageDataRepresentation];
    [library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
        if (!error) {
            [self makeToast:XLocalized(@"保存成功!")];
        }
    }];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
